package month1;

import java.util.Arrays;

public class BuildTree105_05 {
    public static void main(String[] args) {
        System.out.println(buildTree(new int[]{3, 9, 20, 15, 7}, new int[]{9, 3, 15, 20, 7}));
    }

    /**
     * 递归吧
     *
     * @param preorder
     * @param inorder
     * @return
     */
    static TreeNode buildTree(int[] preorder, int[] inorder) {
        int n = preorder.length;
        if (n < 1) {
            return null;
        }
        int i;
        for (i = 0; i < n; ++i) {
            if (inorder[i] == preorder[0]) {
                break;
            }
        }
        return new TreeNode(preorder[0], buildTree(Arrays.copyOfRange(preorder, 1, i + 1), Arrays.copyOfRange(inorder, 0, i + 1)),
                buildTree(Arrays.copyOfRange(preorder, i + 1, n), Arrays.copyOfRange(inorder, i + 1, n)));
    }
}
